﻿using Cache.Aop.Attributes;
using Cache.Aop.Entities;
using Cache.Aop.MyDbContext;
using Microsoft.EntityFrameworkCore;

namespace Cache.Aop.TestServices;

public class UserService : IUserService
{
    private readonly ShopDbContext _dbContext;

    public UserService(ShopDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public async Task<IEnumerable<UserInfo>> GetUserInfo()
    {
        var list = await _dbContext.UserInfo.AsQueryable<UserInfo>().ToListAsync();

        return list;

    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [Caching(AbsoluteExpiration = 10)]
    public async Task<List<UserInfo>> GetListAsync()
    {
        var list = await _dbContext.UserInfo.AsQueryable<UserInfo>().ToListAsync();
        return list;
    }

    /// <summary>
    /// 读取操作
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [Caching(AbsoluteExpiration = 100)]
    public async Task<UserInfo> GetUserInfoAsync(long id)
    {
        var model = await _dbContext.UserInfo.AsQueryable<UserInfo>().Where(it=>it.Id == id).FirstOrDefaultAsync();
        return model;
    }

    /// <summary>
    /// 更新操作
    /// </summary>
    /// <param name="user"></param>
    /// <returns></returns>
    [Caching(AbsoluteExpiration = 0)]
    public async Task<UserInfo> UpdateUserAsync(UserInfo user)
    {
        _dbContext.UserInfo.Update(user);
        var model = _dbContext.UserInfo.FirstOrDefault();
        return model;
    }


}
